<?php

/**
 *      [品牌空间] (C)2001-2010 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: nav.func.php 5120 2010-11-26 06:29:22Z yexinhao $
 */

if(!defined('IN_BRAND')) {
	exit('Access Denied');
}

//取得面包屑式分类导航数组
function getcatnavarray($catid, $type = 'shop') {
	global $_SGLOBAL;
	$thiscatid = 0;
	$catid = intval($catid);
	$navarray = array();
	if($catid>0) {
		if(!isset($_SGLOBAL[$type.'cates']) || !is_array($_SGLOBAL[$type.'cates'])) {
			getcategory($type);
		}
		$categorylist = & $_SGLOBAL[$type.'cates'];
		$thiscatid = $catid;
		while($thiscatid) {
			if(is_array($categorylist[$thiscatid])) {
				$navarray[] = $categorylist[$thiscatid];
				$thiscatid = intval($categorylist[$thiscatid]['upid']);
			} else {
				$thiscatid = 0;
			}
		}
		krsort($navarray);
	}
	return $navarray;
}

function getregionnavarray($regionid) {
	global $_SGLOBAL;
	$regionid = intval($regionid);
	$navarray = array();
	if(!isset($_SGLOBAL['regioncates']) || !is_array($_SGLOBAL['regioncates'])) {
		getcategory('region');
	}
	$categorylist = & $_SGLOBAL['regioncates'];
	//遍历寻找上级节点
	$thisregionid = $lastregionid = $regionid;
	while($thisregionid+1) {
		if(empty($thisregionid)) {
			$navarray[] = getcategorychild($thisregionid, 'region', $lastregionid);
			$thisregionid = -1;
		} elseif(is_array($categorylist[$thisregionid])) {
			if(strcmp($categorylist[$thisregionid]['subcatid'], $thisregionid) !== 0) {
				$navarray[] = getcategorychild($thisregionid, 'region', $lastregionid);
			}
			$lastregionid = $thisregionid;
			$thisregionid = intval($categorylist[$thisregionid]['upid']);
		} else {
			$thisregionid = -1;
		}
	}
	krsort($navarray);
	return $navarray;
}

//获取当前分类的直接子分类
function getcategorychild($catid, $type = 'shop', $currentid = 0) {
	global $_SGLOBAL;
	$catid = intval($catid);
	$catarray = array();
	$top = 0;
	$categorylist = $_SGLOBAL[$type.'cates'];
	if($catid > 0 && is_array($categorylist[$catid])) {
		if(strcmp($categorylist[$catid]['subcatid'], $catid)===0) {
			//最底层分类显示同级分类
			$upid = $categorylist[$catid]['upid'];
			if($upid) {
				$subcatids = $categorylist[$upid]['subcatid'];
			} else {
				$top = 1;
			}
		} else {
			//非最底层也非最顶层的分类，显示子分类
			$subcatids = $categorylist[$catid]['subcatid'];
		}
		if(!$top) {
			$subcatids = explode(',', $subcatids);
			array_shift($subcatids);
			foreach($subcatids as $subcatid) {
				$subcatid = intval(trim($subcatid));
				if($currentid && $subcatid == $currentid) {
					$categorylist[$subcatid]['current'] = 1;
				} else {
					$categorylist[$subcatid]['current'] = 0;
				}
				$catarray[] = $categorylist[$subcatid];
			}
		}
	} else {
		$top = 1;
	}
	if($top) {
		//最顶层分类，显示upid为0的分类
		foreach($categorylist as $cat) {
			if(is_array($cat) && $cat['upid'] == 0) {
				if($currentid && $cat['catid'] == $currentid) {
					$cat['current'] = 1;
				} else {
					$cat['current'] = 0;
				}
				$catarray[] = $cat;
			}
		}
	}
	return $catarray;
}

?>